// app.js

import * as Constant from "./common/constant"

App({
  onLaunch() {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
  },
  globalData: {
    userInfo: null
  },
  addToShopCart(info) {
    return new Promise(resolve => {
      const userInfo = wx.getStorageSync(Constant.USER_INFO);
      if (userInfo && info) {
        let shopCartList = this.getShopCartList();
        let findIndex = shopCartList.findIndex(item => item.iid === info.iid && item.xdSkuId === info.xdSkuId);
        if (findIndex === -1) {
          shopCartList.push(info);
        } else {
          shopCartList[findIndex].purchaseCount += info.purchaseCount;
        }
        wx.setStorageSync(Constant.SHOPPING_CART + userInfo.signature, shopCartList);
        resolve();
      }
    })
  },
  changeGoodsCount(iid, xdSkuId, purchaseCount) {
    return new Promise(resolve => {
      const userInfo = wx.getStorageSync(Constant.USER_INFO);
      if (userInfo) {
        let shopCartList = this.getShopCartList();
        let findIndex = shopCartList.findIndex(item => item.iid === iid && item.xdSkuId === xdSkuId);
        if (findIndex !== -1) {
          shopCartList[findIndex].purchaseCount += purchaseCount;
          wx.setStorageSync(Constant.SHOPPING_CART + userInfo.signature, shopCartList);
        }
      }
    })
  },
  changeGoodsCheck(array) {
    return new Promise(resolve => {
      const userInfo = wx.getStorageSync(Constant.USER_INFO);
      if (userInfo) {
        let shopCartList = this.getShopCartList();
        shopCartList.forEach((item, index) => item.checked = array.includes(index.toString()));
        wx.setStorageSync(Constant.SHOPPING_CART + userInfo.signature, shopCartList);
      }
    })
  },
  changeSelectAll(isSelectAll) {
    return new Promise(resolve => {
      const userInfo = wx.getStorageSync(Constant.USER_INFO);
      if (userInfo) {
        let shopCartList = this.getShopCartList();
        shopCartList.forEach(item => item.checked = isSelectAll);
        wx.setStorageSync(Constant.SHOPPING_CART + userInfo.signature, shopCartList);
      }
    })
  },
  getShopCartList() {
    const userInfo = wx.getStorageSync(Constant.USER_INFO);
    return userInfo && wx.getStorageSync(Constant.SHOPPING_CART + userInfo.signature) || [];
  },
  getUserProfile() {
    return new Promise((resolve, reject) => {
      const userInfo = wx.getStorageSync(Constant.USER_INFO);
      if (userInfo && userInfo.signature) {
        resolve(userInfo);
      }
      reject();
    }).catch(err => {
      return new Promise(resolve => {
        wx.getUserProfile({
          desc: 'desc',
          success: res => {
            const obj = {
              signature: res.signature,
              avatarUrl: res.userInfo.avatarUrl,
              nickName: res.userInfo.nickName
            }
            wx.setStorage({
              key: Constant.USER_INFO,
              data: obj
            })
            resolve(obj);
          }
        })
      })
    })
  }
})